package com.smartview.jboss.spring.model;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.stereotype.Service;

import com.smartview.jboss.spring.dto.NewsDTO;
import com.smartview.jboss.spring.dto.WeatherDTO;

@Service
public class DataFormatter {
	public String formatWeather(WeatherDTO weather) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException {
		// We will format the data using Velocity
		VelocityContext vc = new VelocityContext();
		vc.put("weather", weather);
		
		StringWriter writer = new StringWriter();
		Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("outputWeather.vm"));
		Velocity.evaluate(vc, writer, "", reader);
		return writer.toString();
	}
	
	public String formatNews(NewsDTO news) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException {
		// We will format the data using Velocity
		VelocityContext vc = new VelocityContext();
		vc.put("news", news);
		
		StringWriter writer = new StringWriter();
		Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("outputNews.vm"));
		Velocity.evaluate(vc, writer, "", reader);
		return writer.toString();
	}
}
